package dse.messages.multiversion;

import java.util.LinkedList;

import dse.Experiment;
import dse.ExperimentRecorder;
import dse.Version;
import dse.messages.Message;
import dse.messages.MessageHandler;
import dse.messages.MessageSender;
import dse.messages.MessageType;

public class LatestAndVersionResponseV2MessageHandler extends MessageSender implements
		MessageHandler {
	private LinkedList<Version> versions;
	private Version version;
	private VersionReader reader;
	
	public LatestAndVersionResponseV2MessageHandler(Experiment experiment, LinkedList<Version> versions, Version version, VersionReader reader){
		super(experiment);
		this.versions = versions;
		this.version = version;
		this.reader = reader;
	}
	
	@Override
	public boolean acceptMessage(Message message) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void messageInterupted(ExperimentRecorder recorder, Message message,
			double time) {
		// TODO Auto-generated method stub
		recorder.debug(this, "msgIntrpt: " + message, time);
		reader.readVersion(recorder, time, null, null, message.from());
	}

	@Override
	public void messageReceived(ExperimentRecorder recorder, Message message,
			double time) {
		// TODO Auto-generated method stub
		recorder.debug(this, "msgRcvd: " + message, time);
		if (message.messageType() != MessageType.LatestAndVersionResponseV2) {
			recorder.warn(
					"LatestAndVersionResponseV2MessageHandler recieved " + 
					message.messageType() + " message."
					);
			return;
		}			
		
		reader.readVersion(recorder, time, versions, version, message.from());
	}
}
